<?php
namespace Operationtest\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\EventManager\EventManagerInterface;
use Zend\View\Model\ViewModel;
use Operationtest\Classe\WhereQueryBySelection;
use Operationtest\Classe\OperationDate;
use Operationtest\Classe\Table;
use Zend\Session\Container;

class TableaudebordController extends AbstractActionController
{    
    public function setEventManager(EventManagerInterface $events)
    {
        parent::setEventManager($events);
 
        $controller = $this;
        $events->attach('dispatch', function ($e) use ($controller) {
            $controller->layout()->setTemplate('layout/operationtest');
        }, 100);
 
        return $this;
    }
    
    public function indexAction() {
        $table = new Table($sm = $this->getServiceLocator());
        $operationDate = new OperationDate();
        
        $request = $this->getRequest();        
        $tableArray = array(
            'marcheStoreTable' => $table->getMarchestoreTable(),
            'marcheTable' => $table->getMarcheTable(),
            'storeTable' => $table->getStoreTable()
        );
        
        $whereQueryBySelection = new WhereQueryBySelection($tableArray);
        
        if ($request->isPost()) {
            // INITIALIZE PERIOD ///////////////////////////////////////////////////////////////////////////
            $period = $operationDate->displayPeriod($request);
            
            // REAL ///////////////////////////////////////////////////////////////////////////
            $defaultWhere = $whereQueryBySelection->queryForDashboard($request);
            $meteoWhere = $whereQueryBySelection->queryForDashboard($request, 'date', 'Meteo');
            $ecWhere = $whereQueryBySelection->queryForDashboard($request, 'date', 'StoreCount');
            $rhdataWhere = $whereQueryBySelection->queryForDashboard($request, 'date', 'RhData');
            
            // BUDGETS ///////////////////////////////////////////////////////////////////////////
            $budgetWhere = $whereQueryBySelection->budgetQueryForTableauDeBord($request, 'month');
            $budgetResult = $table->getBudgetTable()->getBudgetForTableauDeBord($budgetWhere);
            $budgets = $budgetTmp = array();
            foreach($budgetResult as $budget) {
                $nbOpenDays = $budget->nbWeek1+$budget->nbWeek2+$budget->nbWeek3+$budget->nbWeek4+$budget->nbWeek5+$budget->nbWeek6;
                $budgetTmp[$budget->month][$budget->idStore]['nbOpenDays'] = $nbOpenDays;
                $budgetTmp[$budget->month][$budget->idStore]['totttc'] = $budget->totttc;
                $budgetTmp[$budget->month][$budget->idStore]['tm'] = $budget->tm;
                $budgetTmp[$budget->month][$budget->idStore]['tttxA'] = $budget->txA + $budget->txAl;
                $budgetTmp[$budget->month][$budget->idStore]['txE'] = $budget->txE;
            }
            
            // Récupération du nombre de jours actuels d'ouverture
            $orders = $table->getSaleheadTable()->getNbOpenDays($defaultWhere['month']);
            $nbOpenDays = $this->getNbOpenDays($orders);
            
            $countBudgetTmp = 1;
            foreach($budgetTmp as $idxtmp => $tmp) {
                foreach($tmp as $idstore) {
                    @$budgets['month']['cattc'] += $nbOpenDays[$idxtmp] * $idstore['totttc'];
                    @$budgets['month']['caht'] += (($idstore['totttc']*0.995/1.07)+($idstore['totttc']*0.005/1.196))*$nbOpenDays[$idxtmp];
                    @$budgets['month']['nbt'] += ($idstore['totttc']/$idstore['tm'])*$nbOpenDays[$idxtmp];
                    @$budgets['month']['tttxA'] += $idstore['tttxA'];
                    @$budgets['month']['txE'] += $idstore['txE'];
                    
                    if($countBudgetTmp == count($budgetTmp)) {
                        @$budgets['day']['cattc'] += $idstore['totttc'];
                        @$budgets['day']['caht'] += (($idstore['totttc']*0.995/1.07)+($idstore['totttc']*0.005/1.196));
                        @$budgets['day']['nbt'] += ($idstore['totttc']/$idstore['tm']);
                        @$budgets['day']['tttxA'] += $idstore['tttxA'];
                        @$budgets['day']['txE'] += $idstore['txE'];
                    }
                }
                $countBudgetTmp++;
            }
            
            $dataDashboard = new Container('dataDashboard');
            $dataDashboard->caMonth = $table->getSaleheadTable()->getTbCa($defaultWhere['month']);
            $dataDashboard->caDay = $table->getSaleheadTable()->getTbCa($defaultWhere['day']);
            $dataDashboard->tmMonth = $table->getSaleheadTable()->getTbTmTtc($defaultWhere['month']);
            $dataDashboard->tmDay = $table->getSaleheadTable()->getTbTmTtc($defaultWhere['day']);
            $dataDashboard->meteoMonth = $table->getMeteoTable()->getMeteos($meteoWhere['month']);
            $dataDashboard->meteoDay = $table->getMeteoTable()->getMeteo($meteoWhere['day']);
            $dataDashboard->mealMonth = $table->getSaleheadTable()->getTbRepas($defaultWhere['month']);
            $dataDashboard->mealDay = $table->getSaleheadTable()->getTbRepas($defaultWhere['day']);
            $dataDashboard->nbTMonth = $table->getSaleheadTable()->getTbTransaction($defaultWhere['month']);
            $dataDashboard->nbTDay = $table->getSaleheadTable()->getTbTransaction($defaultWhere['day']);
            $dataDashboard->nbAMonth = $table->getSalerowTable()->getTbNbArticle($defaultWhere['month']);
            $dataDashboard->nbADay = $table->getSalerowTable()->getTbNbArticle($defaultWhere['day']);
            $dataDashboard->ecMonth = $table->getStorecountTable()->getTbEcarts($ecWhere['month']);
            $dataDashboard->ecDay = $table->getStorecountTable()->getTbEcarts($ecWhere['day']);
            $dataDashboard->acMonth = $table->getSaleheadTable()->getTbAnnulationCaisse($defaultWhere['month']);
            $dataDashboard->acDay = $table->getSaleheadTable()->getTbAnnulationCaisse($defaultWhere['day']);
            $dataDashboard->bqMonth = $table->getStorecountTable()->getTbBanques($ecWhere['month']);
            $dataDashboard->bqDay = $table->getStorecountTable()->getTbBanques($ecWhere['day']);
            $dataDashboard->rhdataMonth = $table->getRhdataTable()->getTbRhdata($rhdataWhere['month']);
            $dataDashboard->rhdataDay = $table->getRhdataTable()->getTbRhdata($rhdataWhere['day']);
            $dataDashboard->budgets = $budgets;
            
            return new ViewModel(array(                
                ///////// PERIODE TEXT
                'periodText' => $operationDate->displayPeriodText($request),
                'tabMonthText' => $operationDate->tabMonthText(),

                ///////// STORE
                'store' => $whereQueryBySelection->getStoresNames($request),

                ///////// CHIFFRE D'AFFAIRES
                'caMonth' => $table->getSaleheadTable()->getTbCa($defaultWhere['month']),
                'caDay' => $table->getSaleheadTable()->getTbCa($defaultWhere['day']),

                ///////// TICHET MOYEN
                'tmMonth' => $table->getSaleheadTable()->getTbTmTtc($defaultWhere['month']),
                'tmDay' => $table->getSaleheadTable()->getTbTmTtc($defaultWhere['day']),
                
                ///////// METEO
                'meteoMonth' => $table->getMeteoTable()->getMeteos($meteoWhere['month']),
                'meteoDay' => $table->getMeteoTable()->getMeteo($meteoWhere['day']),
                
                ///////// REPAS
                'mealMonth' => $table->getSaleheadTable()->getTbRepas($defaultWhere['month']),
                'mealDay' => $table->getSaleheadTable()->getTbRepas($defaultWhere['day']),
                
                ///////// TRANSACTION ET VENTE
                'nbTMonth' => $table->getSaleheadTable()->getTbTransaction($defaultWhere['month']),                
                'nbTDay' => $table->getSaleheadTable()->getTbTransaction($defaultWhere['day']),
                
                'nbAMonth' => $table->getSalerowTable()->getTbNbArticle($defaultWhere['month']),
                'nbADay' => $table->getSalerowTable()->getTbNbArticle($defaultWhere['day']),
                
                ///////// ECART CAISSE
                'ecMonth' => $table->getStorecountTable()->getTbEcarts($ecWhere['month']),
                'ecDay' => $table->getStorecountTable()->getTbEcarts($ecWhere['day']),
                
                ///////// ANNULATION CAISSE
                'acMonth' => $table->getSaleheadTable()->getTbAnnulationCaisse($defaultWhere['month']),
                'acDay' => $table->getSaleheadTable()->getTbAnnulationCaisse($defaultWhere['day']),
                
                ///////// BANQUES
                'bqMonth' => $table->getStorecountTable()->getTbBanques($ecWhere['month']),
                'bqDay' => $table->getStorecountTable()->getTbBanques($ecWhere['day']),  
                
                ///////// MO
                'rhdataMonth' => $table->getRhdataTable()->getTbRhdata($rhdataWhere['month']),
                'rhdataDay' => $table->getRhdataTable()->getTbRhdata($rhdataWhere['day']),
                
                ///////// BUDGETS
                'budgets' => $budgets,
            ));
        } else {
            return $this->redirect()->toRoute('operationtest/default');
        }
    }
    
    public function scoringAction() {
        $table = new Table($sm = $this->getServiceLocator());
        $operationDate = new OperationDate();
        $operationSession = new Container('operationSession');
        
        $request = $this->getRequest();
        $tableArray = array(
            'marcheStoreTable' => $table->getMarchestoreTable(),
            'marcheTable' => $table->getMarcheTable(),
            'storeTable' => $table->getStoreTable()
        );
        
        $whereQueryBySelection = new WhereQueryBySelection($tableArray);
        
        // CLAUSE WHERE
        $defaultWhere = $whereQueryBySelection->queryForDashboardScoring();
        $otherWhere = $whereQueryBySelection->queryForDashboardScoring('date');
        $meteoWhere = $whereQueryBySelection->queryForDashboardScoringMonthOnly('date');

        // CLAUSE GROUPBY
        $groupby = " GROUP BY saleHead.orderDate";            
        $ecgroupby = " GROUP BY date"; 
        $tmgroupby = " GROUP BY saleHead.orderDate"; 
        $monthData = $weekData = $dayData = array();
        $enMonthText = $operationDate->echoMonthtoEn();
        $frMonthText = $operationDate->echoMonthtoShortFr();
        $period = $operationSession->offsetGet('period');
        
        $ca = $table->getSaleheadTable()->getSynthCa($defaultWhere, $groupby); 
        $nbt = $table->getSaleheadTable()->getSynthTransaction($defaultWhere, $groupby);
        $ac = $table->getSaleheadTable()->getSynthAnnulationCaisse($defaultWhere, $groupby);
        foreach($period as $idxm => $month){
            $monthTab = explode(' ', $idxm);
            $year = $monthTab[1];
            $monthText = $enMonthText[$monthTab[0]];
            $monthShortText = $frMonthText[$monthText];
            foreach($month as $idxw => $week){
                foreach($week as $idxd => $day){
                    // CA
                    if(!isset($monthData[$monthShortText.' '.$year]['ca_ttc'])) $monthData[$monthShortText.' '.$year]['ca_ttc'] = 0; 
                    if(!isset($monthData[$monthShortText.' '.$year]['ca_ht'])) $monthData[$monthShortText.' '.$year]['ca_ht'] = 0;
                    if(!isset($weekData[$year][$monthText][$idxw]['ca_ttc'])) $weekData[$year][$monthText][$idxw]['ca_ttc'] = 0; 
                    if(!isset($weekData[$year][$monthText][$idxw]['ca_ht'])) $weekData[$year][$monthText][$idxw]['ca_ht'] = 0;
                    foreach($ca as $ca_day){
                        $orderDate = new \DateTime($ca_day['orderDate']);
                        if($orderDate->format('Y')==$year && $orderDate->format('F')==$monthText && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')){
                            $monthData[$monthShortText.' '.$year]['ca_ttc'] += $ca_day['ca_ttc'];
                            $monthData[$monthShortText.' '.$year]['ca_ht'] += $ca_day['ca_ht'];                            
                            $weekData[$year][$monthText][$idxw]['ca_ttc'] += $ca_day['ca_ttc'];
                            $weekData[$year][$monthText][$idxw]['ca_ht'] += $ca_day['ca_ht'];                            
                            $dayData[$day->format('Y-m-d')]['ca_ttc'] = $ca_day['ca_ttc'];
                            $dayData[$day->format('Y-m-d')]['ca_ht'] = $ca_day['ca_ht'];
                        }
                    }
                    // TRANSACTION
                    if(!isset($monthData[$monthShortText.' '.$year]['nbt'])) $monthData[$monthShortText.' '.$year]['nbt'] = 0;                     
                    if(!isset($weekData[$year][$monthText][$idxw]['nbt'])) $weekData[$year][$monthText][$idxw]['nbt'] = 0;                     
                    foreach($nbt as $nbt_day) {
                        $orderDate = new \DateTime($nbt_day['orderDate']);
                        if($orderDate->format('Y')==$year && $orderDate->format('F')==$monthText && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')){
                            $monthData[$monthShortText.' '.$year]['nbt'] += $nbt_day['nbTransaction'];
                            $weekData[$year][$monthText][$idxw]['nbt'] += $nbt_day['nbTransaction'];
                            $dayData[$day->format('Y-m-d')]['nbt'] = $nbt_day['nbTransaction'];
                        }
                    }
                    // ANNULATION CAISSE
                    if(!isset($monthData[$monthShortText.' '.$year]['ac'])) $monthData[$monthShortText.' '.$year]['ac'] = 0;   
                    if(!isset($monthData[$monthShortText.' '.$year]['mac'])) $monthData[$monthShortText.' '.$year]['mac'] = 0;   
                    if(!isset($weekData[$year][$monthText][$idxw]['ac'])) $weekData[$year][$monthText][$idxw]['ac'] = 0; 
                    if(!isset($weekData[$year][$monthText][$idxw]['mac'])) $weekData[$year][$monthText][$idxw]['mac'] = 0;

                    foreach($ac as $ac_day) {
                        $orderDate = new \DateTime($ac_day['orderDate']);
                        if($orderDate->format('Y')==$year && $orderDate->format('F')==$monthText && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')){
                            $monthData[$monthShortText.' '.$year]['ac'] += $ac_day['ac'];
                            $monthData[$monthShortText.' '.$year]['mac'] += $ac_day['montant_ac'];
                            $weekData[$year][$monthText][$idxw]['ac'] += $ac_day['ac'];
                            $weekData[$year][$monthText][$idxw]['mac'] += $ac_day['montant_ac'];
                            $dayData[$day->format('Y-m-d')]['ac'] = $ac_day['ac'];
                            $dayData[$day->format('Y-m-d')]['mac'] = $ac_day['montant_ac'];
                        }
                    }
                }
            }
        }
//        echo '<pre>';
//        print_r($monthData);    
//        echo '</pre>';
        
        // Traitement pour les jours
        foreach($dayData as $date => $value) {
            $dayCattc[$date] = floatval(isset($value['ca_ttc'])?$value['ca_ttc']:0);
            $dayCaht[$date] = floatval(isset($value['ca_ht'])?$value['ca_ht']:0);
            $dayNbt[$date] = floatval(isset($value['nbt'])?$value['nbt']:0);
            $dayAc[$date] = floatval(isset($value['ac'])?$value['ac']:0);
            $dayMac[$date] = floatval(isset($value['mac'])?$value['mac']:0);
        }
        
        foreach($dayCattc as $date => $value) {
            $dateObject = new \DateTime($date);
            if($value == max($dayCattc)) {
                $maxCattcDay = $dateObject->format('d-M-Y');
            }
            if($value == min($dayCattc)) {
                $minCattcDay = $dateObject->format('d-M-Y');
            }
        }
        foreach($dayCaht as $date => $value) {
            $dateObject = new \DateTime($date);
            if($value == max($dayCaht)) {
                $maxCahtDay = $dateObject->format('d-M-Y');
            }
            if($value == min($dayCaht)) {
                $minCahtDay = $dateObject->format('d-M-Y');
            }
        }
        foreach($dayNbt as $date => $value) {
            $dateObject = new \DateTime($date);
            if($value == max($dayNbt)) {
                $maxNbtDay = $dateObject->format('d-M-Y');
            }
            if($value == min($dayNbt)) {
                $minNbtDay = $dateObject->format('d-M-Y');
            }
        }
        foreach($dayAc as $date => $value) {
            $dateObject = new \DateTime($date);
            if($value == max($dayAc)) {
                $maxAcDay = $dateObject->format('d-M-Y');
            }
            if($value == min($dayAc)) {
                $minAcDay = $dateObject->format('d-M-Y');
            }
        }
        foreach($dayMac as $date => $value) {
            $dateObject = new \DateTime($date);
            if($value == max($dayMac)) {
                $maxMacDay = $dateObject->format('d-M-Y');
            }
            if($value == min($dayMac)) {
                $minMacDay = $dateObject->format('d-M-Y');
            }
        }
        
        // Traitement pour les semaines
        foreach ($weekData as $y => $year) {
            foreach ($year as $m => $month) {
                $wcount = 1;
                foreach ($month as $weeknum) {
                    if($weeknum['ca_ttc'] !=0 )
                        $weekCattc[$y.'_'.$m.'_'.$wcount] = $weeknum['ca_ttc'];
                    if($weeknum['ca_ht'] !=0 )
                        $weekCaht[$y.'_'.$m.'_'.$wcount] = $weeknum['ca_ht'];
                    if($weeknum['nbt'] !=0 )
                        $weekNbt[$y.'_'.$m.'_'.$wcount] = $weeknum['nbt'];
                    if($weeknum['ac'] !=0 )
                        $weekAc[$y.'_'.$m.'_'.$wcount] = $weeknum['ac'];
                    if($weeknum['mac'] !=0 )
                        $weekMac[$y.'_'.$m.'_'.$wcount] = $weeknum['mac'];
                    $wcount++;
                }
            }
        }
        
        foreach($weekCattc as $idx => $w) {
            if($w == max($weekCattc)) {
                $t = explode('_', $idx);
                $maxCattcWeek = $frMonthText[$t[1]].' '.$t[0].'/Semaine '.$t[2];
            }
            if($w == min($weekCattc)) {
                $t = explode('_', $idx);
                $minCattcWeek = $frMonthText[$t[1]].' '.$t[0].'/Semaine '.$t[2];
            }
        }
        foreach($weekCaht as $idx => $w) {
            if($w == max($weekCaht)) {
                $t = explode('_', $idx);
                $maxCahtWeek = $frMonthText[$t[1]].' '.$t[0].'/Semaine '.$t[2];
            }
            if($w == min($weekCaht)) {
                $t = explode('_', $idx);
                $minCahtWeek = $frMonthText[$t[1]].' '.$t[0].'/Semaine '.$t[2];
            }
        }
        foreach($weekNbt as $idx => $w) {
            if($w == max($weekNbt)) {
                $t = explode('_', $idx);
                $maxNbtWeek = $frMonthText[$t[1]].' '.$t[0].'/Semaine '.$t[2];
            }
            if($w == min($weekNbt)) {
                $t = explode('_', $idx);
                $minNbtWeek = $frMonthText[$t[1]].' '.$t[0].'/Semaine '.$t[2];
            }
        }
        foreach($weekAc as $idx => $w) {
            if($w == max($weekAc)) {
                $t = explode('_', $idx);
                $maxAcWeek = $frMonthText[$t[1]].' '.$t[0].'/Semaine '.$t[2];
            }
            if($w == min($weekAc)) {
                $t = explode('_', $idx);
                $minAcWeek = $frMonthText[$t[1]].' '.$t[0].'/Semaine '.$t[2];
            }
        }
        foreach($weekMac as $idx => $w) {
            if($w == max($weekMac)) {
                $t = explode('_', $idx);
                $maxMacWeek = $frMonthText[$t[1]].' '.$t[0].'/Semaine '.$t[2];
            }
            if($w == min($weekMac)) {
                $t = explode('_', $idx);
                $minMacWeek = $frMonthText[$t[1]].' '.$t[0].'/Semaine '.$t[2];
            }
        }
        //var_dump($weekMac);
        
        // Traitement pour les mois
        foreach($monthData as $month => $value) {
            $monthCattc[$month] = floatval(isset($value['ca_ttc'])?$value['ca_ttc']:0);
            $monthCaht[$month] = floatval(isset($value['ca_ht'])?$value['ca_ht']:0);
            $monthNbt[$month] = floatval(isset($value['nbt'])?$value['nbt']:0);
            $monthAc[$month] = floatval(isset($value['ac'])?$value['ac']:0);
            $monthMac[$month] = floatval(isset($value['mac'])?$value['mac']:0);
        }
        
        foreach($monthCattc as $month => $value) {
            if($value == max($monthCattc)) {
                $maxCattcMonth = $month;
            }
            if($value == min($monthCattc)) {
                $minCattcMonth = $month;
            }
        }
        foreach($monthCaht as $month => $value) {
            if($value == max($monthCaht)) {
                $maxCahtMonth = $month;
            }
            if($value == min($monthCaht)) {
                $minCahtMonth = $month;
            }
        }
        foreach($monthNbt as $month => $value) {
            if($value == max($monthNbt)) {
                $maxNbtMonth = $month;
            }
            if($value == min($monthNbt)) {
                $minNbtMonth = $month;
            }
        }
        foreach($monthAc as $month => $value) {
            if($value == max($monthAc)) {
                $maxAcMonth = $month;
            }
            if($value == min($monthAc)) {
                $minAcMonth = $month;
            }
        }
        foreach($monthMac as $month => $value) {
            if($value == max($monthMac)) {
                $maxMacMonth = $month;
            }
            if($value == min($monthMac)) {
                $minMacMonth = $month;
            }
        }
        
        $variables = array(
            'meteoMonth' => $table->getMeteoTable()->getMeteos($meteoWhere),
            
            // Pour les jours
            'maxDCattc' => max($dayCattc),
            'maxDCaht' => max($dayCaht),
            'maxDNbt' => max($dayNbt),
            'maxDAc' => max($dayAc),
            'maxDMac' => max($dayMac),
            
            'minDCattc' => min($dayCattc),
            'minDCaht' => min($dayCaht),
            'minDNbt' => min($dayNbt),
            'minDAc' => min($dayAc),
            'minDMac' => min($dayMac),
            
            'maxCattcDay' => $maxCattcDay,
            'maxCahtDay' => $maxCahtDay,
            'maxNbtDay' => $maxNbtDay,
            'maxAcDay' => $maxAcDay,
            'maxMacDay' => $maxMacDay,
            
            'minCattcDay' => $minCattcDay,
            'minCahtDay' => $minCahtDay,
            'minNbtDay' => $minNbtDay,
            'minAcDay' => $minAcDay,
            'minMacDay' => $minMacDay,
            
            // Pour les semaines
            'maxWCattc' => max($weekCattc),
            'maxWCaht' => max($weekCaht),
            'maxWNbt' => max($weekNbt),
            'maxWAc' => max($weekAc),
            'maxWMac' => max($weekMac),
            
            'minWCattc' => min($weekCattc),
            'minWCaht' => min($weekCaht),
            'minWNbt' => min($weekNbt),
            'minWAc' => min($weekAc),
            'minWMac' => min($weekMac),
            
            'maxCattcWeek' => $maxCattcWeek,
            'maxCahtWeek' => $maxCahtWeek,
            'maxNbtWeek' => $maxNbtWeek,
            'maxAcWeek' => $maxAcWeek,
            'maxMacWeek' => $maxMacWeek,
            
            'minCattcWeek' => $minCattcWeek,
            'minCahtWeek' => $minCahtWeek,
            'minNbtWeek' => $minNbtWeek,
            'minAcWeek' => $minAcWeek,
            'minMacWeek' => $minMacWeek,
            
            // Pour les mois
            'maxMCattc' => max($monthCattc),
            'maxMCaht' => max($monthCaht),
            'maxMNbt' => max($monthNbt),
            'maxMAc' => max($monthAc),
            'maxMMac' => max($monthMac),
            
            'minMCattc' => min($monthCattc),
            'minMCaht' => min($monthCaht),
            'minMNbt' => min($monthNbt),
            'minMAc' => min($monthAc),
            'minMMac' => min($monthMac),
            
            'maxCattcMonth' => $maxCattcMonth,
            'maxCahtMonth' => $maxCahtMonth,
            'maxNbtMonth' => $maxNbtMonth,
            'maxAcMonth' => $maxAcMonth,
            'maxMacMonth' => $maxMacMonth,
            
            'minCattcMonth' => $minCattcMonth,
            'minCahtMonth' => $minCahtMonth,
            'minNbtMonth' => $minNbtMonth,
            'minAcMonth' => $minAcMonth,
            'minMacMonth' => $minMacMonth,
        );
        $viewModel = new ViewModel();
        $viewModel->setTerminal(true);
        $viewModel->setVariables($variables);
        return $viewModel;
    }
    
    public function getNbOpenDays($orders) {
        $months = array();
        foreach($orders as $order):
            $date = new \DateTime($order['orderDate']);
            @$months[$date->format('Y-m').'-01'] += $order['nbOpenDays'];
        endforeach;
        return $months;
    }
}
?>
